<?php
/**
 * Created by PhpStorm.
 * User: yiduan
 * Date: 2019/2/15 0015
 * Time: 14:34
 */
namespace app\app\controller;
use think\Db;
use think\Exception;

class Save extends Common{

    function __construct(\think\App $app = null)
    {
        $this->requestType = 'post';
        parent::__construct($app);
    }

    function index(){
        $data = $this->getPgData('collections');
        if(!$data){
            return 0;
        }
        $data = json_decode($data, true);

        $db = Db::name('yoo_video');
        $inData = [];
        foreach ($data as $row){
            if(Db::name('yoo_video')->where("vid='".$row['tvBoard']['videoData']['vid']."'")->find()){
                continue;
            }
            $inData[] = array(
                'vid' => $row['tvBoard']['videoData']['vid'],
                'title' => $row['tvBoard']['videoData']['title'],

                'userId' => $row['tvBoard']['user']['userInfo']['account']['id'],
                'userName' => $row['tvBoard']['user']['userInfo']['userName'],
                'faceImageUrl' => $row['tvBoard']['user']['userInfo']['faceImageUrl'],
                'gifUrl' => @$row['tvBoard']['poster']['gifUrl'] ? @$row['tvBoard']['poster']['gifUrl'] : '',
                'shareImgUrl' => $row['tvBoard']['shareItem']['shareImgUrl'],
                'shareUrl' => $row['tvBoard']['shareItem']['shareUrl'],
                'timeStamp' => $row['tvBoard']['timeStamp'],
                'info' => json_encode($row)

            );
        }
        try{
            if($db->insertAll($inData)){
                return 1;
            }else{
                return 0;
            }
        }catch (Exception $e){
            print_r($inData);
        }

    }

    /**
     * 更新downUrl
     * @return int
     */
    function downUrl(){
        $id = $this->getPgData('id');
        $url = $this->getPgData('url');

        if(Db::name('yoo_video')->where("id=$id")->setField('downUrl', $url)){
            return 1;
        }
        return 0;

    }
}